/**
 *
 * @description: 解构赋值
 * @author: junyong.hong
 * @createTime: 2018/3/27
 * @version: 1.0.0.0
 * @history:
 *    1、
 *    2、
 *
 */
// 数组解构赋值
{
    let a, b, rest;
    [a, b] = [1, 2];
    // 输出1 2
    console.log(a, b);
}

{
    let a, b, rest;
    [a,b,...rest]=[1,2,3,4,5,6]
    // 输出1 2 [3,4,5,6]
    console.log(a,b,rest);
}


// 对象解构赋值
{
    let a,b;
    ({a, b} = {a:1,b:2});
    // 输出1 2
    console.log(a,b)
}

// 没有配对成功，默认值为3；如果=3去掉，c是undefined
{
    let a, b, c, rest;
    [a, b, c=3] = [1, 2];
    // 输出1 2 3
    console.log(a, b, c);
}

// 变量交换
{
    let a = 1;
    let b = 2;
    [a, b] = [b, a];
    // 输出2 1
    console.log(a, b)
}

// 取值方便
{
    function f() {
        return [1,2]
    }
    let a, b;
    [a,b]=f();
    // 输出1 2
    console.log(a, b)
}

// 选择性接收变量
{
    function f() {
        return [1,2,3,4,5]
    }
    let a,b,c;
    // 两个逗号，对应一个值
    [a,,,b]=f();
    // 输出1 4
    console.log(a, b)
}


{
    function f() {
        return [1,2,3,4,5]
    }
    let a,b,c;
    [a,...b]=f();
    // 输出1 [2,3,4,5]
    console.log(a, b)
}


{
    let o={p:42,q:true};
    let {p,q}=o;
    // 输出：42 true
    console.log(p,q);
}

{
    let {a = 10, b = 5} = {a: 3};
    // 输出：3 5
    console.log(a, b);
}


// 获取json
{
    let metaData = {
        title: 'abc',
        test:[{
            title:'test',
            desc:'description'
        }]
    };

    // 接收的变量esTitle；
    let {title:esTitle,test:[{title:cnTitle}]} = metaData;
    // 输出：abc test
    console.log(esTitle, cnTitle)
}




